package org.alex.samples.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.alex.samples.model.ExamScore;
import org.alex.samples.model.PagerModel;
import org.alex.samples.model.SystemContext;
import org.alex.samples.model.User;
import org.alex.samples.model.UserSubject;
import org.alex.samples.model.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class IUserDaoImpl extends BaseHibernateDAO<User> implements IUserDao {

	public List<User> findAllUsers() throws Exception {
		return loadAll();
	}

	public UserVO getUserById(String id) throws Exception {
		User user = get(id);
		UserVO userVO = new UserVO();
		BeanUtils.copyProperties(user, userVO);
		List<UserSubject> userSubjects = new ArrayList<UserSubject>();
		for (UserSubject userSubject : user.getUserSubject()) {
			userSubjects.add(userSubject);
		}
		userVO.setUserSubjects(userSubjects);

		List<ExamScore> examScores = new ArrayList<ExamScore>();
		for (ExamScore score : user.getExamScore()) {
			examScores.add(score);

		}
		userVO.setExamScores(examScores);
		return userVO;
	}

	public void createUser(User user) throws Exception {
		save(user);
	}

	@Override
	public void createUsers(List<User> userList) throws Exception {
		save(userList);
	}

	public void editUser(User user) throws Exception {
		update(user);
	}

	public void deletetUser(String id) throws Exception {
		deleteByKey(id);
	}

	@Override
	public Iterator iterate(String queryString, Object[] values) {
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public PagerModel<UserVO> findAllByPage() {
		PagerModel<UserVO> page = new PagerModel<UserVO>();
		ArrayList<UserVO> datas = new ArrayList<UserVO>();
		int offset = SystemContext.getOffset();
		int pageSize = SystemContext.getSize();

		// 使用hibernate完成分页
		List<User> tempDates = this.getSession().createQuery("form User").setFirstResult(offset).setMaxResults(pageSize)
				.list();
		for (User user : tempDates) {
			UserVO userVO = new UserVO();
			BeanUtils.copyProperties(user, userVO);
			datas.add(userVO);
		}
		page.setDatas(datas);
		page.setOffset(offset);
		page.setPageSize(pageSize);

		// 获取总记录数
		long totalSize = (long) this.getSession().createQuery("select count(*) from User").uniqueResult();
		page.setTotalSize(totalSize);
		return page;
	}

	/*
	 * new
	 * SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(user.getLastLockTime())
	 */
	@Override
	public void updataUserLastLockTime(User user) throws Exception {
		String hql = "update User set lastLockTime='"
				+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(user.getLastLockTime()) + "' where id='"
				+ user.getId() + "'";
		this.getSession().createQuery(hql).executeUpdate();
	}

	@Override
	public void updataUserLoginFailCount(User user) throws Exception {
		String hql = "update User set loginFailCount=" + user.getLoginFailCount() + " where id='" + user.getId() + "'";
		this.getSession().createQuery(hql).executeUpdate();
	}
	
	
}